package com.kyzh.core.pager.weal.vip

import android.app.Activity
import android.content.Intent
import android.content.res.ColorStateList
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gushenge.core.beans.CentreBean
import com.gushenge.core.impls.VipImpl
import com.jeremyliao.liveeventbus.LiveEventBus
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.databinding.ActivityVipcentreBinding
import com.kyzh.core.databinding.TablayoutVipTextBinding
import com.kyzh.core.utils.loadImage
import com.kyzh.core.utils.setVisibility
import kotlinx.android.synthetic.main.activity_vipcentre.*
import org.jetbrains.anko.textColor


class VipCentreActivity : BaseActivity() {
    companion object{
        @JvmStatic
        fun start(context: Activity) {
            val starter = Intent(context, VipCentreActivity::class.java)
            context.startActivityForResult(starter,1000)
        }
    }
    private lateinit var binding:ActivityVipcentreBinding
    private var datas : CentreBean?= null

    private var myDengJi : Int = 0  //控制显示的等级
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding =DataBindingUtil.setContentView<ActivityVipcentreBinding>( this,R.layout.activity_vipcentre,)
        titleView.setBg(R.drawable.bg_standard)
        initData()
    }
    fun initData(){
        VipImpl.getVipCentre {
            datas =  this
            datas?.let {
                initTop(it.top)
                initTab()
            }
        }
    }

    fun initTop(top : CentreBean.Top){
        binding.apply {
            tvName.text = top.name
            iconHead.loadImage(top.head)
            tvTime.text = top.end_time
            if(top.vip == 0){
                tvOpen.text = "开通"
            }else{
                tvOpen.text = "续费"
            }
            progress.progress = top.exp*100/top.last_exp
            tvExp.text = String.format(resources.getString(R.string.exotips),top.exp,top.last_exp -top.exp )
            imgUp.loadImage(top.style_img)
            if(top.style_exp.contains("-")){
                tvUp.textColor = resources.getColor(R.color.exp_red)
            }else{
                tvUp.textColor = resources.getColor(R.color.exp_green)
            }
            tvUp.text =top.style_exp
            imgUp.setVisibility(!top.style_img.isNullOrEmpty())
            tvUp.setVisibility(!top.style_exp.isNullOrEmpty())
            igBg.loadImage(top.head_frame)
            tvOpen.setOnClickListener {
//                if (startLogin()) {
//                    startActivity<BrowserActivity>(
//                        GlobalKeys.TITLE to "${tvOpen.text.toString()}vip",
//                        GlobalKeys.LINK to top.pay_url
//                    )
//                }
                OpenVipActivity.start(this@VipCentreActivity)
                finish()
            }
            LiveEventBus.get<String?>("frame_img").observe(this@VipCentreActivity){
                igBg.loadImage(it)
            }

        }


    }

    fun initTab(){

        val adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount() = datas!!.nav.size

            override fun createFragment(position: Int): Fragment = when(position){
                0 -> VipCentreFragment.newInstance(datas!!.nav[position].type)  //vip礼包
                1 -> VipCentreHeardFragment.newInstance(datas!!.nav[position].type) //头像框
                2 -> VipCentreHeardFragment.newInstance(datas!!.nav[position].type) //选材昵称
                3 -> VipCentreCouponFragment.newInstance(datas!!.nav[position].type) //优惠券
                4 -> VipCentreHeardFragment.newInstance(datas!!.nav[position].type) //优惠券
                else -> VipCentreFragment.newInstance(datas!!.nav[position].type) //勋章
            }
        }
//        table.getTabAt(1)?.apply {
//            customView = null
//            customView = TablayoutVipTextBinding.inflate(layoutInflater).also {
//                it.tv.text = text
//            }.root
//        }
        viewpage.adapter = adapter
        viewpage.offscreenPageLimit = 4
        val mediator = TabLayoutMediator(
            table,
            viewpage
        ){ tab, position->
            tab.customView = TablayoutVipTextBinding.inflate(layoutInflater).also {
                it.tv.text = datas!!.nav[position].name
                tab.text = datas!!.nav[position].name
                it.tv.textColor = resources.getColor(R.color.font_99)
            }.root
        }
        mediator.attach()

        table.tabRippleColor = ColorStateList.valueOf(resources.getColor(R.color.white1))
        table.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                TablayoutVipTextBinding.inflate(layoutInflater).apply {
                    tab?.customView = null
                    tv.text = tab?.text
                    tv.textColor = resources.getColor(R.color.font_33)
                    tv.textSize = 14f
                    view.setVisibility(true)
                    tab?.customView = root
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                TablayoutVipTextBinding.inflate(layoutInflater).apply {
                    tab?.customView = null
                    tv.text = tab?.text
                    tv.textSize = 12f
                    tv.textColor = resources.getColor(R.color.font_99)
                    view.visibility = View.INVISIBLE
                    tab?.customView = root
                }
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
                TablayoutVipTextBinding.inflate(layoutInflater).apply {
                    tab?.customView = null
                    tv.text = tab?.text
                    tv.textSize = 14f
                    tv.textColor = resources.getColor(R.color.font_33)
                    view.setVisibility(true)
                    tab?.customView = root
                }
            }
        })
        table.post {
            table.selectTab(table.getTabAt(0))
        }
    }
}